<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未二维码添加标题</title>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/axios.min.js"></script>
    <script>
        function excelXMLExport() {
            $.ajax({
                url: "/excelXMLExport",
                type: "post",
                data: {},
                async: false,
                cache: false,
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8'
                },
                responseType: 'blob',
                responseEncoding: 'utf8',
                success: function (res) {
                    var fileName = "电话号码台账--" + Date.now() + ".xlsx";
                    let blob = new Blob([res], {type: "application/vnd.ms-excel;charset=UTF-8"});
                    // IE
                    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                        window.navigator.msSaveOrOpenBlob(blob, fileName)
                    } else {
                        let objectUrl = (window.URL || window.webkitURL).createObjectURL(blob)
                        let downFile = document.createElement('a')
                        downFile.style.display = 'none'
                        downFile.href = objectUrl
                        // 下载后文件名
                        downFile.download = fileName
                        document.body.appendChild(downFile)
                        downFile.click()
                        // 下载完成移除元素
                        document.body.removeChild(downFile)
                        // 只要映射存在，Blob就不能进行垃圾回收，因此一旦不再需要引用，就必须小心撤销URL，释放掉blob对象。
                        window.URL.revokeObjectURL(objectUrl)
                    }
                },
            });
        }

        function excelXlsxExport() {
            axios.post('/excelXlsxExport', {}, { responseType: 'blob' }).then(res => {
                var fileName = "电话号码台账-EXCEL-" + Date.now() + ".xlsx";
                let blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                // let blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=UTF-8'});
                // IE
                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob, fileName)
                } else {
                    let objectUrl = (window.URL || window.webkitURL).createObjectURL(blob)
                    let downFile = document.createElement('a')
                    downFile.style.display = 'none'
                    downFile.href = objectUrl
                    // 下载后文件名
                    downFile.download = fileName
                    document.body.appendChild(downFile)
                    downFile.click()
                    // 下载完成移除元素
                    document.body.removeChild(downFile)
                    // 只要映射存在，Blob就不能进行垃圾回收，因此一旦不再需要引用，就必须小心撤销URL，释放掉blob对象。
                    window.URL.revokeObjectURL(objectUrl)
                }
            })
        }

    </script>
</head>
<body>

    <div>
        <button onclick="excelXMLExport()">excelXMLExport导出</button>
        <a href="/excelXMLExport">excelXMLExport</a>
    </div>

    <div>
        <button onclick="excelXlsxExport()">excelXlsxExport导出</button>
        <a href="/excelXlsxExport">excelXlsxExport</a>
    </div>

</body>
</html>
